package artid.com.arttogo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.LotSearchActivity;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.MySharedPreferences;

public class LotSearchInputAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	CallbackSky cs;
	public ArrayList<String> list;
	MySharedPreferences sharedPreferences;
	public LotSearchInputAdapter(Context mContext, ArrayList<String> list) {
		this.mContext = mContext;
		sharedPreferences = MySharedPreferences.getMySharedPreferences(mContext);
		mInflater = LayoutInflater.from(mContext);
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return 0;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public void addList(ArrayList<String> list) {
		this.list.addAll(list);
		notifyDataSetChanged();
	}
	public void setList(ArrayList<String> list) {
		this.list = list;
		notifyDataSetChanged();
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		System.out.println();
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_lot_search, null);
			 vh.text = (TextView) convertView
			 .findViewById(R.id.item_lotsrh_text);
			 vh.imgDel = (ImageView)convertView.findViewById(R.id.item_lotsrh_del);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		vh.text.setText(list.get(position));
		vh.imgDel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				list.remove(position);
				notifyDataSetChanged();
				String strs = "";
				for (int i = 0; i < list.size(); i++) {
					strs+=(list.get(i)+"#");
				}
				sharedPreferences.setValue(LotSearchActivity.LOTSRH_TAG, strs);
			}
		});
		return convertView;
	}
	
	

	class ViewHolder {
		TextView text;
		ImageView imgDel;

	}
}
